*******************************************************************************
* Specify city and years; load data
*******************************************************************************

local city = "fort_worth"

local y1 2009
local y2 2014
local y3 2020

use dataset_`city'_cross, clear

*******************************************************************************
* Rescale explanatory variables
*******************************************************************************

foreach x in pctnhblack distancem {
	su `x'
	replace `x' = (`x'- r(min)) / (r(max) - r(min))
}
replace distancem = -distancem + 1

*******************************************************************************
* Time interactions
*******************************************************************************

gen pctnhblackXy2 = pctnhblack*year_2
gen pctnhblackX2020 = pctnhblack*year_3
gen distanceXy2 = distancem*year_2
gen distanceX2020 = distancem*year_3

label variable pctnhblackXy2 "Share Black X 2014"
label variable pctnhblackX2020 "Share Black X 2020"
label variable distanceXy2 "Proximity to Shooting X 2014"
label variable distanceX2020 "Proximity to Shooting X 2020"

*******************************************************************************
* Do the regressions
*******************************************************************************

eststo clear

eststo: reg turnout pctnhblack pctnhblackXy2 pctnhblackX2020 ///
	distancem distanceXy2 distanceX2020 year_2 year_3 x_c y_c ///
	[aw=registeredvoters], robust 
estadd local yfe = "X"
estadd local wtvote = "X"
estadd local clvote = ""
estadd local latlon = "X"
estadd local myse = "Robust"
	
foreach l in 1 2 5 {
	eststo: acreg turnout pctnhblack pctnhblackXy2 pctnhblackX2020 ///
		distancem distanceXy2 distanceX2020 ///
		x_c y_c year_2 year_3 lregisteredvoters, ///
		spatial latitude(y_c) longitude(x_c) distcutoff(`l') ///
		hac id(precinctyear) time(year) lagcutoff(0)
	estadd local slag = `l'
	estadd local yfe = "X"
	estadd local wtvote = ""
	estadd local clvote = "X"
	estadd local latlon = "X"
	estadd local myse = "HAC"
}

*******************************************************************************
* Output table
*******************************************************************************

#delimit;

esttab
	using "_output/table A20.tex"
	,
		replace
		booktabs
		label
		nomtitles
		collabels(none)
		drop(*_c lreg* year_2 year_3 _cons)
		cells(b(fmt(3) star) se(par))
		width(\linewidth)
		stats(yfe wtvote clvote latlon myse slag N, 
			label(
				"Year Fixed Effects"
				"Weight by Registered Voters"
				"Control for Log(Registered Voters)"
				"Control for Latitude \& Longitude"
				"Standard Errors"
				"Distance Cutoff (Miles)"
				"Number of Precincts" 
			)
			fmt(%20.0f %20.0f))
		compress
		alignment(l)	
		gap		
		;
			
#delimit cr
	
*******************************************************************************
* End
*******************************************************************************
